package com.hsjk.szwj.module.train.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hsjk.szwj.framework.common.constant.CommonConstant;
import com.hsjk.szwj.framework.common.exception.ServiceException;
import com.hsjk.szwj.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.hsjk.szwj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hsjk.szwj.module.pay.api.IMchDivisionReceiverApi;
import com.hsjk.szwj.module.pay.api.dto.MchDivisionReceiverDTO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminSetSkuDivisionReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminSkuDivisionListRespVO;
import com.hsjk.szwj.module.train.convert.SkuDivisionConvert;
import com.hsjk.szwj.module.train.dal.dataobject.product.SkuDivisionMappingDO;
import com.hsjk.szwj.module.train.dal.mysql.product.ISkuDivisionMappingMapper;
import com.hsjk.szwj.module.train.service.ISkuDivisionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/12/04 22:50
 **/
@Service
public class SkuDivisionServiceImpl implements ISkuDivisionService {
    @Resource
    private ISkuDivisionMappingMapper skuDivisionMappingMapper;
    @Resource
    private IMchDivisionReceiverApi mchDivisionReceiverApi;

    @Override
    public Boolean batchCreateSkuDivision(List<AdminSetSkuDivisionReqVO> reqVOS) {
        boolean preCheck = true;
        if (CollUtil.isNotEmpty(reqVOS)) {
            for (AdminSetSkuDivisionReqVO vo : reqVOS) {
                if (CommonConstant.DIVISION_MODEL_PERCENTAGE.equals(vo.getDivisionModel())) {
                    if (ObjectUtil.isNull(vo.getDivisionProfit())) {
                        preCheck = false;
                    }
                } else if (CommonConstant.DIVISION_MODEL_AMOUNT.equals(vo.getDivisionModel())) {
                    if (ObjectUtil.isNull(vo.getDivisionAmount())) {
                        preCheck = false;
                    }
                }
            }
        }
        if (!preCheck) {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                    , "参数不正确");
        }
        for (AdminSetSkuDivisionReqVO vo : reqVOS) {
            if (CommonConstant.DIVISION_MODEL_PERCENTAGE.equals(vo.getDivisionModel())) {
                vo.setDivisionProfit(vo.getDivisionProfit() / 100);
            }
        }
        List<SkuDivisionMappingDO> skuDivisionMappingDOS = SkuDivisionConvert.INSTANCE.convertSkuDivisionMappings(reqVOS);
        skuDivisionMappingMapper.insertBatch(skuDivisionMappingDOS);
        return Boolean.TRUE;
    }

    @Override
    public Boolean createSkuDivision(AdminSetSkuDivisionReqVO reqVO) {
        SkuDivisionMappingDO skuDivisionMappingDO = SkuDivisionConvert.INSTANCE.convertSkuDivisionMappingDO(reqVO);
        int insert = skuDivisionMappingMapper.insert(skuDivisionMappingDO);
        return insert > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public List<AdminSkuDivisionListRespVO> getAdminSkuDivisionListBySkuId(Long skuId) {
        List<SkuDivisionMappingDO> skuDivisionMappingDOS = skuDivisionMappingMapper.selectList(new LambdaQueryWrapperX<SkuDivisionMappingDO>()
                .eq(SkuDivisionMappingDO::getSkuId, skuId));
        List<AdminSkuDivisionListRespVO> adminSkuDivisionListRespVOS = SkuDivisionConvert.INSTANCE.convertAdminSkuDivisionListRespVO(skuDivisionMappingDOS);
        if (CollUtil.isNotEmpty(adminSkuDivisionListRespVOS)) {
            for (AdminSkuDivisionListRespVO vo : adminSkuDivisionListRespVOS) {
                if (CommonConstant.DIVISION_MODEL_PERCENTAGE.equals(vo.getDivisionModel())) {
                    vo.setDivisionProfit(vo.getDivisionProfit() * 100);
                }
                MchDivisionReceiverDTO mchDivisionReceiverById = mchDivisionReceiverApi.getMchDivisionReceiverById(vo.getMchDivisionReceiverId());
                if (ObjectUtil.isNotNull(mchDivisionReceiverById)) {
                    vo.setReceiverAlias(mchDivisionReceiverById.getReceiverAlias());
                    vo.setAccType(mchDivisionReceiverById.getAccType());
                }
            }
        }
        return adminSkuDivisionListRespVOS;
    }

}
